package com.byzk.p2p.admin.web.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.byzk.p2p.admin.base.util.PageInfo;
import com.byzk.p2p.admin.web.dao.ArticleDao;
import com.byzk.p2p.admin.web.dao.ArticleTypeDao;
import com.byzk.p2p.admin.web.dto.Article;
import com.byzk.p2p.admin.web.dto.ArticleType;
import com.byzk.p2p.admin.web.vo.ArticleVo;

@Service
public class ArticleService {

	@Autowired
	private ArticleDao articleDao;
	@Autowired
	private ArticleTypeDao articleTypeDao;
	
	/**
	 * 分页获取文章
	 * @param index
	 * @param limit
	 * @return
	 */
	public PageInfo<ArticleVo> articleList(Map<String, Object> queryMap,Integer index,Integer limit) {
		if (index != null && limit != null) {
			if (queryMap == null) {
				queryMap = new HashMap<String, Object>();
			}
			queryMap.put("start", (index-1)*limit);
			queryMap.put("limit", limit);
		}
		Integer total = articleDao.count(queryMap);
		List<ArticleVo> list = articleDao.articleList(queryMap);
		PageInfo<ArticleVo> pageInfo = new PageInfo<ArticleVo>(total, index, limit);
		pageInfo.setPageList(list);
		return pageInfo;
	}
	
	/**
	 * 保存文章
	 * @param article 文章
	 * @return
	 */
	public Integer saveOrUpdateArticle(Article article) {
		if (article.getId() == null) {
			return articleDao.insert(article);
		}else{
			return articleDao.update(article);
		}
	}
	
	/**
	 * 获取文章
	 * @param articleId 文章id
	 * @return
	 */
	public ArticleVo getArticleById(Integer articleId){
		return articleDao.getArticleById(articleId);
	}
	
	/**
	 * 获取相邻文章
	 * @param queryMap (匹配符：gt；lt...)
	 * @return
	 */
	public Article getAdjacentArticle(Map<String,Object> queryMap){
		return articleDao.getAdjacentArticle(queryMap);
	}
	
	/**
	 * 删除文章
	 * @param articleId 文章id
	 * @return
	 */
	public Integer deleteArticleById(Integer articleId){
		return articleDao.delete(articleId);
	}
	
	/**
	 * 所有的文章类目
	 * @return
	 */
	public List<ArticleType> articleTypeList(){
		return articleTypeDao.articleTypeList();
	}
	
	/**
	 * 获取最新6条公告
	 * @param type 文章类型id
	 * @return
	 */
	public List<Article> list6News(Integer typeId){
		return articleDao.list6News(typeId);
	}
	
	/**
	 * 	获取公告
	 * @param typeId 文章类型id
	 * @param limit 需要的条数
	 * @return
	 */
	public List<Article> listNewsByType(Integer typeId, Integer limit) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("typeId", typeId);
		params.put("limit", limit);
		return articleDao.listNewsByType(params);
	}
	/**
	 * 获取文章时间轴
	 * @return
	 */
	public List<ArticleVo> newsTime(){
		return articleDao.createTimeList();
	}

	public Integer getIdByType(String articleType) {
		// TODO Auto-generated method stub
		return articleDao.getIdByType(articleType);
	}
}
